<template>
  <MainPage title="文件规则库">
    <div class="content">
      <!-- <div class="search">
        <el-input
          v-model="keyWord"
          style="width: 240px"
          placeholder="请输入ID编码或规则集名称"
          @keyup.native.enter="getTableData"
        ></el-input>
        <el-button type="primary" @click="getTableData">搜索</el-button>
        <el-button type="text" @click="clearClick">清空搜索</el-button>
      </div> -->
      <div class="table-wrap">
        <FormTable ref="formTable" :table-label="tableLabel" :table-data="tableData">
          <template #ruleSetName="{ row }">{{ row.ruleSetName }}规则集</template>
          <template #operation="slotProps">
            <el-link
              type="primary"
              style="margin-left: 12px"
              :underline="false"
              @click="handleClick(slotProps.row)"
            >
              打开规则集
            </el-link>
          </template>
        </FormTable>
      </div>
    </div>
    <fileRuleSetDetail
      v-if="showRuleSetDetail"
      ref="fileRuleSetDetail"
      type="file"
      :conditional-relation-option="conditionalRelationOption"
      :variable-model-option="variableModelOption"
      :fact-mode-to-comparator-option="factModeToComparatorOption"
      @close="handleClose"
    />
  </MainPage>
</template>

<script>
import {
  loadRuleSetGrid,
  getConditionalRelationOptionList,
  getModelOptionList,
  getFactModeToComparatorOptionList
} from '@/api/rule-management'
import fileRuleSetDetail from './components/fileRuleSetDetail.vue'
import FormTable from '@/components/form-table/index.vue'
import defaultSettings from '@/settings'
import { generateUUID, postMessage, receivedMessage } from '@/utils/index'
export default {
  name: 'FileRuleLibrary',
  components: { fileRuleSetDetail, FormTable },
  data() {
    return {
      isLoading: false,
      keyWord: '',
      tableLabel: [
        {
          prop: 'ruleSetId',
          label: 'ID编码'
        },
        {
          prop: 'ruleSetName',
          label: '规则集名称',
          type: 'slot'
        },
        {
          prop: 'updateTime',
          label: '更新日期'
        },
        {
          prop: 'operation',
          type: 'slot',
          width: 200,
          label: '操作'
        }
      ],
      tableData: [],
      conditionalRelationOption: [],
      variableModelOption: [],
      factModeToComparatorOption: [],
      showRuleSetDetail: false,
      loadRuleSetGridRequestId: generateUUID(),
      getConditionalRelationOptionListRequestId: generateUUID(),
      getModelOptionListRequestId: generateUUID(),
      getFactModeToComparatorOptionListRequestId: generateUUID()
    }
  },

  mounted() {
    this.getTableData()
    this.getConditionalRelationOptionList()
    this.getModelOptionList()
    this.getFactModeToComparatorOptionList()
    receivedMessage(this.dataHandle)
  },

  methods: {
    dataHandle(result) {
      let self = this
      let e = defaultSettings.isWPF ? result : { data: JSON.parse(result) }
      if (e.data.webInfo.requestId === self.loadRuleSetGridRequestId) {
        self.tableData = e.data.response.data
        self.isLoading = false
      } else if (e.data.webInfo.requestId === self.getConditionalRelationOptionListRequestId) {
        self.conditionalRelationOption = e.data.response.data
      } else if (e.data.webInfo.requestId === self.getModelOptionListRequestId) {
        self.variableModelOption = e.data.response.data.parameterModelDataList
      } else if (e.data.webInfo.requestId === self.getFactModeToComparatorOptionListRequestId) {
        self.factModeToComparatorOption = e.data.response.data
      }
    },
    getConditionalRelationOptionList() {
      if (defaultSettings.isWPF || defaultSettings.isCEF) {
        this.getConditionalRelationOptionListRequestId = generateUUID()
        let message = {
          RequestUrl: process.env.VUE_APP_URL + '/ruleSet/getConditionalRelationOptionList',
          RequestMethod: 'POST',
          Headers: {
            contentType: 'application/json'
          },
          QueryParameter: {},
          RequestBody: {},
          WebInfo: {
            requestId: this.getConditionalRelationOptionListRequestId
          }
        }
        postMessage(message)
      } else {
        getConditionalRelationOptionList().then((res) => {
          this.conditionalRelationOption = res.data
        })
      }
    },
    getModelOptionList() {
      if (defaultSettings.isWPF || defaultSettings.isCEF) {
        this.getModelOptionListRequestId = generateUUID()
        let message = {
          RequestUrl: process.env.VUE_APP_URL + '/ruleSet/getModelOptionList',
          RequestMethod: 'POST',
          Headers: {
            contentType: 'application/json'
          },
          QueryParameter: {},
          RequestBody: {},
          WebInfo: {
            requestId: this.getModelOptionListRequestId
          }
        }
        postMessage(message)
      } else {
        getModelOptionList().then((res) => {
          this.variableModelOption = res.data.parameterModelDataList
        })
      }
    },
    getFactModeToComparatorOptionList() {
      if (defaultSettings.isWPF || defaultSettings.isCEF) {
        this.getFactModeToComparatorOptionListRequestId = generateUUID()
        let message = {
          RequestUrl: process.env.VUE_APP_URL + '/ruleSet/getFactModeToComparatorOptionList',
          RequestMethod: 'POST',
          Headers: {
            contentType: 'application/json'
          },
          QueryParameter: {},
          RequestBody: {},
          WebInfo: {
            requestId: this.getFactModeToComparatorOptionListRequestId
          }
        }
        postMessage(message)
      } else {
        getFactModeToComparatorOptionList().then((res) => {
          this.factModeToComparatorOption = res.data
        })
      }
    },
    getTableData() {
      this.isLoading = true
      if (defaultSettings.isWPF || defaultSettings.isCEF) {
        this.loadRuleSetGridRequestId = generateUUID()
        let message = {
          RequestUrl: process.env.VUE_APP_URL + '/ruleSet/loadGrid',
          RequestMethod: 'POST',
          Headers: {
            contentType: 'application/json'
          },
          QueryParameter: {},
          RequestBody: { searchName: this.keyWord, ruleBaseType: 1 },
          WebInfo: {
            requestId: this.loadRuleSetGridRequestId
          }
        }
        postMessage(message)
      } else {
        loadRuleSetGrid({ searchName: this.keyWord, ruleBaseType: 1 })
          .then(({ data }) => {
            this.tableData = data
          })
          .finally(() => {
            this.isLoading = false
          })
      }
    },
    clearClick() {
      this.keyWord = ''
      this.getTableData()
    },
    handleClick(row) {
      this.showRuleSetDetail = true
      this.$nextTick(() => {
        this.$refs.fileRuleSetDetail.is('edit', row)
      })
    },
    handleClose() {
      this.showRuleSetDetail = false
      this.getTableData()
    }
  }
}
</script>

<style lang="less" scoped>
.content {
  height: 100%;
  .table-wrap {
    height: 100%;
  }
}
</style>
